﻿Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Public Character As Integer
    Public PlayerName As String
    Public Port As Integer
    Public COMPORTSTATUS As Boolean
    Public Portname As String
    Public ConnectioStatus As Integer


    Public PadEvents As SerialPort = New System.IO.Ports.SerialPort("COM1", _
                                                                        9600, _
                                                                        Parity.None, _
                                                                        8, _
                                                                        StopBits.One)
    Public Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'Check if  port is open and then close it
        If PadEvents.IsOpen Then
            PadEvents.Close()
        End If
    End Sub

    Public Function OpenPort() As Boolean
        If Not PadEvents.IsOpen Then
            PadEvents.Open()
        End If
        If PadEvents.IsOpen Then
            Return True
        Else
            Return False
        End If
    End Function


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ADD ITEMS TO COM PORTS COMBOBOX
        Dim PortCount As Integer
        For PortCount = 1 To 20
            ComboBox1.Items.Add("COM" & PortCount)
        Next
        ComboBox1.SelectedIndex = 0

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        LabelPLAYER.Text = "STAN"
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        LabelPLAYER.Text = "KYLE"
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        LabelPLAYER.Text = "KENNY"
    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
        LabelPLAYER.Text = "BUTTERS"
    End Sub

    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        LabelPLAYER.Text = "ROBERT"
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        MsgBox("UNLOCK IT FIRST")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (LabelPLAYER.Text <> "NO PLAYER SELECTED" And TexName.Text <> "") Then
            Select Case LabelPLAYER.Text
                Case "STAN"
                    Character = 1
                Case "KYLE"
                    Character = 2
                Case "KENNY"
                    Character = 3
                Case "BUTTERS"
                    Character = 4
                Case "ROBERT"
                    Character = 5
            End Select
            Portname = "COM" & ComboBox1.SelectedIndex + 1
            PlayerName = TexName.Text
            'OPEN PORT AND TEST PORT CONNECTION
            Form3.ShowDialog()
            If (ConnectioStatus = 1) Then
                Form2.ShowDialog()
            Else
                MsgBox("I CAN´T CONNECT TO GAMEPAD")
            End If
        Else
            MsgBox("PLEASE COMPLETE THE FORM")
        End If
    End Sub

End Class
